package com.chuangjiangx.member.basic.web.config;

import com.github.pagehelper.PageInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * @author zhangquanquan on 2018/7/2.
 */
@Slf4j
@MapperScan(basePackages = {
        "com.chuangjiangx.member.**.mapper",
        "com.chuangjiangx.partner.platform.dao",
//        "com.cloudrelation.partner.platform.dao",
//        "com.cloudrelation.merchant.dao",
//        "com.chuangjiangx.member.dao.dal"
//        "com.chuangjiangx.merchant",
//        "com.chuangjiangx.promote"
})
@EnableTransactionManagement
public class MybatisConfig {

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);

        //mybatis 配置
        Configuration configuration = new Configuration();
        configuration.setCacheEnabled(true);
        configuration.setDefaultStatementTimeout(3000);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setUseGeneratedKeys(true);
        configuration.addInterceptor(pageInterceptor());
        sessionFactory.setConfiguration(configuration);
        //扫描mybatis的xml文件
        PathMatchingResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
        List<Resource> resources = new ArrayList<>();
        try {
            resources.addAll(Arrays.asList(resourceLoader.getResources("classpath*:com/chuangjiangx/member/**/*Mapper.xml")));
            resources.addAll(Arrays.asList(resourceLoader.getResources("classpath*:com/chuangjiangx/partner/platform/**/*Mapper.xml")));
//            resources.addAll(Arrays.asList(resourceLoader.getResources("classpath*:com/chuangjiangx/**/mapper/*Mapper.xml")));
//            resources.addAll(Arrays.asList(resourceLoader.getResources("classpath*:com/cloudrelation/**/mapper/*Mapper.xml")));
//            //处理会员member-dao中的相关XML不能注入问题
//            resources.addAll(Arrays.asList(resourceLoader.getResources("classpath*:com/chuangjiangx/**/mapper/**/*Mapper.xml")));

        } catch (IOException e) {
            log.error(e.getMessage());
        }
        sessionFactory.setMapperLocations(resources.toArray(new Resource[resources.size()]));
        return sessionFactory.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean
    public PageInterceptor pageInterceptor(){
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect","mysql");
        properties.setProperty("offsetAsPageNum","true");
        properties.setProperty("rowBoundsWithCount","true");
        properties.setProperty("pageSizeZero","false");
        properties.setProperty("reasonable","false");
        // properties.setProperty("params","pageNum=pageHelperStart;pageSize=pageHelperRows;");
        properties.setProperty("supportMethodsArguments","false");
        properties.setProperty("returnPageInfo","always");
        pageInterceptor.setProperties(properties);
        return pageInterceptor;
    }

}
